package fr.inserm.um915.jimpute.model;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;
import com.sleepycat.je.DatabaseEntry;

public class BuildChromPosition
	implements Comparable<BuildChromPosition>
	{
	private byte build;
	private byte chrom;
	private int position;
	
	public static class BuildChromPositionComparator implements Comparator<byte[]>
		{
		@Override
		public int compare(byte[] o1, byte[] o2)
			{
			Binding b=new BuildChromPosition.Binding();
			BuildChromPosition s1= b.entryToObject(new DatabaseEntry(o1));
			BuildChromPosition s2= b.entryToObject(new DatabaseEntry(o2));
			return s1.compareTo(s2);
			}
		}
	
	public static class Binding extends TupleBinding<BuildChromPosition> 
		{
		@Override
		public void objectToEntry(BuildChromPosition o, TupleOutput out)
			{
			out.writeByte((byte)o.build);
			out.writeByte((byte)o.chrom);
			out.writeInt(o.position);
			}
			
		@Override
		public BuildChromPosition entryToObject(TupleInput in)
			{
			byte b= in.readByte();
			byte s= in.readByte();
			int p=in.readInt();
			return new BuildChromPosition(b,s, p);
			}
		};
	

		public static class ArrayBinding extends TupleBinding<List<BuildChromPosition>> 
			{
			private Binding bind=new Binding();
			@Override
			public void objectToEntry(List<BuildChromPosition> L, TupleOutput out)
				{
				out.writeInt(L.size());
				for(BuildChromPosition o:L)
					{
					bind.objectToEntry(o, out);
					}
				}
				
			@Override
			public List<BuildChromPosition> entryToObject(TupleInput in)
				{
				int n=in.readInt();
				List<BuildChromPosition> L=new ArrayList<BuildChromPosition>(n);
				for(int i=0;i< n;++i)
					{
					L.add(bind.entryToObject(in));
					}
				return L;
				}
			};	
		
		
	public static final Binding	BINDING=new Binding();
	public static final ArrayBinding	ARRAY_BINDING=new ArrayBinding();
	
	public BuildChromPosition(BuildChromPosition cp)
		{
		this.build=cp.build;
		this.chrom=cp.chrom;
		this.position=cp.position;
		}

	
	public BuildChromPosition(byte build,byte chrom,int position)
		{
		this.build=build;
		this.chrom=chrom;
		this.position=position;
		}
		

	
	@Override
	public int compareTo(BuildChromPosition o)
		{
		int i= build-(o.build);
		if(i!=0) return i;
		i= chrom-(o.chrom);
		if(i!=0) return i;
		i= position-o.position;
		return i;
		}
	
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + build;
		result = prime * result + chrom;
		result = prime * result + position;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BuildChromPosition other = (BuildChromPosition) obj;
		return this.compareTo(other)==0;
	}
	
	public byte getBuildIdx()
		{
		return build;
		}
	
	public byte getChromosomeIdx()
		{
		return this.chrom;
		}
	
	public int getPosition()
		{
		return position;
		}
	
	@Override
	public String toString()
		{
		return "("+Build.values()[this.build].name()+")"+
			Chromosome.values()[this.chrom].name()+
			":"+position;
		}
	
	}